package com.qianjun.hadoop.test;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;

public class TextPair implements WritableComparable<TextPair> {

	private Text first;
	private Text second;

	public TextPair() {
	}

	public TextPair(Text first, Text second) {
		this.first = first;
		this.second = second;
	}

	public TextPair(String first, String second) {
		this.first = new Text(first);
		this.second = new Text(second);
	}

	@Override
	public void write(DataOutput out) throws IOException {
		// TODO Auto-generated method stub
		first.write(out);
		second.write(out);
	}

	@Override
	public void readFields(DataInput in) throws IOException {
		// TODO Auto-generated method stub
		first.readFields(in);
		second.readFields(in);
	}

	@Override
	public int compareTo(TextPair o) {

		int cmp = first.compareTo(o.first);
		if (cmp != 0) {
			return cmp;
		}
		return second.compareTo(o.second);

	}

	@Override
	public int hashCode() {

		return first.hashCode() * 163 + second.hashCode();

	}

	@Override
	public boolean equals(Object obj) {

		if (obj instanceof TextPair) {
			return first.equals(((TextPair) obj).first) && second.equals(((TextPair) obj).second);

		}
		return false;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return first + "\t" + second;
	}

}
